/**
 * 
 */
package com.p2p.front.tender;

import java.util.List;

import org.springframework.stereotype.Service;

import com.p2p.base.constents.InnerAccountConsts;
import com.p2p.base.exception.BizException;
import com.p2p.base.model.Money;
import com.p2p.front.dal.model.LoanInfo;
import com.p2p.front.dal.model.LoanVariation;
import com.p2p.front.dal.model.Tender;
import com.p2p.trans.client.enums.SubTransCodeEnum;
import com.p2p.trans.client.enums.TransCodeEnum;
import com.p2p.trans.client.model.TransInfo;
import com.p2p.trans.client.result.AccountTransResult;

/**
 * @author ruiqi
 *
 */
@Service("flowLoanTender")
public class FlowLoanTender extends AbstractTenderBiz {

	/* (non-Javadoc)
	 * @see com.p2p.user.tender.AbstractTenderBiz#doUserTrans(java.lang.String, com.p2p.base.model.Money, java.lang.String)
	 */
	@Override
	protected void doUserTrans(String accountNo, Money amount, String bizNo) {
		TransInfo transInfo = new TransInfo();
		transInfo.setTransCode(TransCodeEnum.TXCODE_TRANSFER);
		transInfo.setSubTransCode(SubTransCodeEnum.SUB_TRANSFER_TENDER);
		transInfo.setTransAccountOut(accountNo);
		transInfo.setTransAmount(amount);
		transInfo.setTransMemo("投标");
		AccountTransResult transResult = this.remoteAccountTransFacade.transfer(transInfo);
		if(!transResult.isSuccess()){
			throw new BizException(transResult.getResultCode());
		}
	}

	@Override
	protected void afterTenderProcessing(Tender tender) {
		asyncBiz.asyncCreateTenderReturn(tender,loanInfoService,tenderReturnService);
	}
	

	
	@Override
	protected void doExtraTenderFullBiz(List<Tender> tenderList, LoanInfo loanInfo) {
		
	}

	@Override
	public void doLoanMoney(LoanVariation loanStatus, LoanInfo loanInfo) {
		
		TransInfo transInfo = new TransInfo();
		transInfo.setTransCode(TransCodeEnum.TXCODE_TRANSFER);
		transInfo.setSubTransCode(SubTransCodeEnum.SUB_TRANSFER_TENDER_COLLECT);
		//流转标转账到内部归集账户
		transInfo.setTransAccountIn(InnerAccountConsts.innerTenderAccountNo);
		transInfo.setTransAmount(loanStatus.getTenderAmount());
		transInfo.setTransMemo("流转标归集");
		AccountTransResult result = this.remoteAccountTransFacade.transfer(transInfo);
		if(!result.isSuccess()){
			logger.error("流转标归集账务错误:loanId="+loanInfo.getLid());
			return;
		}
		super.doCreatLoanRepay(loanStatus, loanInfo);
		
		/*boolean bool= loanVariationService.updateStatusForRepay(loanStatus.getLoanId());
		if(!bool){
			logger.error("更新放款状态失败:loanId="+loanInfo.getLid());
		}*/
	}

	@Override
	protected void doExtraLoanFullBiz(LoanVariation loanStatus, LoanInfo loanInfo) {
		
		//流转标直接放款
		this.doLoanMoney(loanStatus, loanInfo);
	}

	@Override
	public void doTenderExpiredBiz(LoanVariation LoanVariation) {
		this.doTenderFullBiz(LoanVariation.getLoanId());
		
	}
}
